package com.debai.controller;

import com.debai.pojo.Position;
import com.debai.service.PositionService;
import com.debai.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/position")
public class PositionController {
    @Autowired
    private PositionService positionService;


    //增加门店
    @PostMapping("/insert")
    public Result insert(@RequestBody Position position,@RequestHeader(name = "Authorization") String token){
        String positionName = position.getPositionName();
        if (positionService.selectByName(positionName)!=null){
            return Result.error("门店已存在!!!");
        }else {
            int i = positionService.insert(position, token);
            if (i >= 1) {
                return Result.success();
            } else {
                return Result.error("插入失败!!!");
            }
        }
    }

    //查看所有门店
    @GetMapping("/selectAllPosition")
    public Result<List<Position>> selectAll(){
        List<Position> positions = positionService.selectAll();
        return Result.success(positions);
    }



    //根据id进行删除
    @DeleteMapping
    public Result delete(@RequestParam(name = "id") int id){
        int i = positionService.delete(id);
        if (i>=1){
            return Result.success();
        }else {
            return Result.error("删除失败");
        }
    }

    //根据id进行修改
    @PutMapping
    public Result update(@RequestBody Position position){
        int i = positionService.update(position);
        if (i>=1){
            return Result.success();
        }else {
            return Result.error("修改失败");
        }
    }
}
